class Solution {
public:
    struct Compare {
        bool operator()(const pair<int, int>& p1, const pair<int, int>& p2) {
            return p1.second < p2.second;
        }
    };
    vector<int> topKFrequent(vector<int>& nums, int k) {
        priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> q;
        vector<int> v;
        unordered_map<int, int> mp;
        for (const auto& e : nums)
            mp[e]++;
        for (const auto& e : mp) {
            q.push({e.first, e.second});
        }
        while (k--) {
            v.push_back(q.top().first);
            q.pop();
        }
        return v;
    }
};